﻿package editorPackage {
	
	import flash.display.MovieClip;
	import flash.net.FileReference;
	import flash.events.*;
	import flash.net.FileFilter;
	import flash.net.URLRequest;
	import flash.display.Loader;

	public class ImageLoader extends EventDispatcher {
	
		private var imageHolder:MovieClip;
		private var loader:Loader;
		private var fr:FileReference;
		public var filename:String;
	
		public function ImageLoader() {
			imageHolder = new MovieClip();
			loader = new Loader();
			fr = new FileReference();
		}
		
		public function loadImage():void {
			fr.browse(new Array(new FileFilter("JPG", "*.jpg"),new FileFilter("JPEG", "*.jpeg"),new FileFilter("GIF", "*.gif"),new FileFilter("PNG", "*.png"),new FileFilter("BMP", "*.bmp")));
			fr.addEventListener(Event.SELECT, beginLoad);
		}
		
		public function beginLoad(e:Event) {
			filename = fr.name;
			//trace(filename);
			loader.load(new URLRequest("images/" + fr.name));
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
		}
		
		public function loadSpecific(fn:String) {
			filename = fn;
			loader.load(new URLRequest("images/" + fn));
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
		}
		
		private function imageLoaded(e:Event):void {
			imageHolder.addChild(loader);
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function imageLoading(e:ProgressEvent):void {
			//trace("loading image: " + (e.bytesLoaded / e.bytesTotal * 100.0) + "%") 
		}
		
		public function getImage():MovieClip { return imageHolder; }
	
	}
}